﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

using Base64 = System.String;

namespace CloudReader.Update
{
    public class VersionProvider
    {
        List<string> _files;
        public VersionProvider()
        {
            _files = new List<string>();
        }
        public void RegistryFile(string file)
        {
            _files.Add(file);
        }
        public void MakeVersion()
        {
            List<Tuple<string, Base64>> result=new List<Tuple<string, Base64>>();
            Task t = new Task(() =>
            {
                foreach (var i in _files)
                {
                    result.Add(new Tuple<string, Base64>(i, getMd5(i)));
                }
                OnProcessCompleted?.Invoke(result);
            });
            t.Start();
        }
        static Base64 getMd5(string file)
        {
            var md5 = new MD5Cng();
            if (File.Exists(file))
            {
                var data = File.ReadAllBytes(file);

                return Convert.ToBase64String(md5.ComputeHash(data));
            }
            else
            {
                return string.Empty;
            }
        }
        public event Action<List<Tuple<string, string>>> OnProcessCompleted;
    }
}
